/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Main;

import java.awt.HeadlessException;
import java.awt.event.KeyEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;

/**
 *
 * @author gean
 */
public class Login extends javax.swing.JFrame {

    
    private final Connection conn;
    private ResultSet rs=null;
    
    public Login() {
        initComponents();
        conn =gestion_chambes_hotel.Gestion_Chambes_Hotel.DB_Connection();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel3 = new javax.swing.JLabel();
        Username = new javax.swing.JTextField();
        Passwd = new javax.swing.JPasswordField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMaximumSize(new java.awt.Dimension(393, 271));
        setMinimumSize(new java.awt.Dimension(393, 271));
        setUndecorated(true);
        setPreferredSize(new java.awt.Dimension(393, 271));
        setResizable(false);
        getContentPane().setLayout(null);

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Main/Images/Labels/enter_login.png"))); // NOI18N
        jLabel3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jLabel3MouseReleased(evt);
            }
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel3MouseClicked(evt);
            }
        });
        jLabel3.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jLabel3KeyPressed(evt);
            }
        });
        getContentPane().add(jLabel3);
        jLabel3.setBounds(190, 190, 120, 49);

        Username.setFont(new java.awt.Font("Droid Sans Fallback", 1, 14)); // NOI18N
        Username.setForeground(new java.awt.Color(153, 153, 153));
        Username.setText("Username");
        Username.setToolTipText("");
        Username.setBorder(null);
        Username.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                UsernameMouseClicked(evt);
            }
        });
        Username.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                UsernameActionPerformed(evt);
            }
        });
        getContentPane().add(Username);
        Username.setBounds(100, 80, 170, 30);

        Passwd.setFont(new java.awt.Font("Droid Sans Fallback", 1, 14)); // NOI18N
        Passwd.setForeground(new java.awt.Color(153, 153, 153));
        Passwd.setText("*********");
        Passwd.setBorder(null);
        Passwd.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                PasswdMouseClicked(evt);
            }
        });
        Passwd.addInputMethodListener(new java.awt.event.InputMethodListener() {
            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
            }
            public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
                PasswdCaretPositionChanged(evt);
            }
        });
        Passwd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PasswdActionPerformed(evt);
            }
        });
        Passwd.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                PasswdKeyReleased(evt);
            }
        });
        getContentPane().add(Passwd);
        Passwd.setBounds(100, 150, 170, 30);

        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Main/Images/Labels/username.png"))); // NOI18N
        getContentPane().add(jLabel5);
        jLabel5.setBounds(90, 70, 220, 50);

        jLabel6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Main/Images/Labels/passwd.png"))); // NOI18N
        getContentPane().add(jLabel6);
        jLabel6.setBounds(90, 130, 220, 70);

        jLabel7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Main/Images/Labels/mot-pass-oublier.png"))); // NOI18N
        jLabel7.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jLabel7MouseReleased(evt);
            }
        });
        getContentPane().add(jLabel7);
        jLabel7.setBounds(40, 230, 150, 30);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Main/Images/Labels/login_back.png"))); // NOI18N
        getContentPane().add(jLabel1);
        jLabel1.setBounds(0, -10, 400, 290);

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents
    
    
    private void login()
    {
    
        String user=Username.getText();
        String pass=new String(Passwd.getPassword());
        
        try {
            
            int id=1;
            String sql_login="select * from utilisateur where LOGIN=? and MOT_PASSE=?";
            String utilisateur_connecter="update utilisateur_connecter set username=? where ID=?";
            
            
            
            PreparedStatement ps_login=conn.prepareStatement(sql_login);
            PreparedStatement ps_util = conn.prepareStatement(utilisateur_connecter);
            
            ps_util.setString(1, user);
            ps_util.setInt(2, 1);
            
            ps_login.setString(1, user);
            ps_login.setString(2, pass);
            rs=ps_login.executeQuery();
            if(rs.next())
            {
                
                
                   Main m =new Main();
                   m.setVisible(true);
                   setVisible(false);
                   ps_util.executeUpdate();
             
            }
            else{
                JOptionPane.showMessageDialog(null, "Username incorrect ou mot de passe incorrect!");
            }
            
            
            
        } catch (SQLException | HeadlessException e) {
            
            JOptionPane.showMessageDialog(null, e.getMessage());
        }
    }
                
    
    private void jLabel3MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel3MouseReleased
        login();
    }//GEN-LAST:event_jLabel3MouseReleased

    private void jLabel3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel3MouseClicked
        
    }//GEN-LAST:event_jLabel3MouseClicked

    private void UsernameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_UsernameActionPerformed
       // Username.setText("");
    }//GEN-LAST:event_UsernameActionPerformed

    private void PasswdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PasswdActionPerformed
       // Passwd.setText("");
    }//GEN-LAST:event_PasswdActionPerformed

    private void UsernameMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_UsernameMouseClicked
        Username.setText("");
    }//GEN-LAST:event_UsernameMouseClicked

    private void PasswdMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_PasswdMouseClicked
        Passwd.setText("");
    }//GEN-LAST:event_PasswdMouseClicked

    private void jLabel7MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel7MouseReleased
        
        
        remettre re =new remettre();
        re.setVisible(true);
        setVisible(false);
        
    }//GEN-LAST:event_jLabel7MouseReleased

    private void PasswdCaretPositionChanged(java.awt.event.InputMethodEvent evt) {//GEN-FIRST:event_PasswdCaretPositionChanged
         Passwd.setText("");
    }//GEN-LAST:event_PasswdCaretPositionChanged

    private void jLabel3KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jLabel3KeyPressed
        
        
    }//GEN-LAST:event_jLabel3KeyPressed

    private void PasswdKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_PasswdKeyReleased
       if(evt.getKeyCode()==KeyEvent.VK_ENTER){
             login();
        }
    }//GEN-LAST:event_PasswdKeyReleased

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Login().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPasswordField Passwd;
    private javax.swing.JTextField Username;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    // End of variables declaration//GEN-END:variables
}
